<?php
session_start();
if (!isset($_SESSION['login']))
{
        echo "<meta http-equiv='refresh' content='0;url=index.php'>";
}
if (isset($_GET['delete'])) 
{
	$id = $_REQUEST['delete'];
	
	$connection = mysql_connect('localhost', 'panospet', 'meipei2c');
	if (!$connection) {
	    die('Could not connect: ' . mysql_error());
	}						
	mysql_query("SET NAMES utf8");
	mysql_select_db("panospet");
	
		
	$query = "SELECT * FROM ukol7_bmi WHERE id = '".$id."' ";
	$result = mysql_query($query);
	if (!$result) {
	    die('Chyba ve spusteni dotazu: ' . mysql_error());
	}
	$row = mysql_fetch_array($result); 
  
	if($row['user_id'] == $_SESSION['id'])
	{
		$query = "DELETE FROM ukol7_bmi WHERE id = '".$id."' LIMIT 1";
		$result = mysql_query($query);
		if (!$result) {
		    die('Chyba ve spusteni dotazu: ' . mysql_error());
		}
		else
		{
			$error_mess = "Úspěšně vymazáno";
		}
	}
	else
	{
		$error_mess = "Pro tuto akci nemáte oprávnění.";	
	}
}


	$connection = mysql_connect('localhost', 'panospet', 'meipei2c');
	if (!$connection) {
	    die('Could not connect: ' . mysql_error());
	}						
	mysql_query("SET NAMES utf8");
	mysql_select_db("panospet");
	
		
	$query = "SELECT * FROM ukol7_bmi WHERE user_id = '".$_SESSION['id']."' ";
	$result = mysql_query($query);
	if (!$result) {
	    die('Chyba ve spusteni dotazu: ' . mysql_error());
	}	

	if($result)
	{	
            echo "<h2>Výpis měření</h2>";
            if($error_mess)
            {
                echo "<p>".$error_mess."</p>";
            }
            echo "<table>";
            echo "<tr>";
            echo "<th>Datum</th>";
            echo "<th>BMI</th>";
            echo "<th></th>";
            echo "<th></th>";
            while ($row = mysql_fetch_array($result)) 
            {
                $zaznam = true;
                echo "<tr>";
                    echo "<td>".$row['date']."</td>";
                    echo "<td>".$row['bmi']."</td>";
                    echo "<td><a href=\"index.php?cmd=4&detail=".$row['id']."\"><input type=\"button\" value=\"Detail\"></a></td>";
                    echo "<td><a href=\"index.php?cmd=3&delete=".$row['id']."\"><input type=\"button\" value=\"Vymazat\"></a></td>";                    
                echo "</tr>";
            }
            echo "</table>";
            
                      
            if(!$zaznam)
            {
                echo "<p>Žádná měření</p>";
            }
	}
?>


			